//
//  YXReplyModel.m
//  CreditCard
//
//  Created by 杨旭 on 2018/1/24.
//  Copyright © 2018年 杨旭. All rights reserved.
//

#import "YXReplyModel.h"
#import <NSAttributedString+YYText.h>
@implementation YXReplyModel
-(NSAttributedString *)attributedText {
    
    if ([self.replyAgentId isEqualToString:[YXUserInfoManager getUserInfo].ID]) {
        NSString *textString = [NSString stringWithFormat:@"%@",self.replyAgentContents];
        
        NSMutableAttributedString *mutableAttributedString = [[NSMutableAttributedString alloc] initWithString:textString];
        mutableAttributedString.yy_font = [UIFont systemFontOfSize:12];
        mutableAttributedString.yy_color = color_globalColor;
        mutableAttributedString.yy_lineSpacing = 10.0f;
        return mutableAttributedString;
    }
    
    if (self.repliesAgentName.length > 0) {
        // 有回复
        NSString *textString = [NSString stringWithFormat:@"%@回复%@: %@", self.repliesAgentName, self.replyAgentName, self.replyAgentContents];;
        NSMutableAttributedString *mutableAttributedString = [[NSMutableAttributedString alloc] initWithString:textString];
        mutableAttributedString.yy_font = [UIFont systemFontOfSize:12];
        mutableAttributedString.yy_color = color_textOne;
        mutableAttributedString.yy_lineSpacing = 10.0f;
        
        NSRange fromUserRange = NSMakeRange(0, self.repliesAgentName.length);
        YYTextHighlight *fromUserHighlight = [YYTextHighlight highlightWithBackgroundColor:[UIColor colorWithWhite:0.000 alpha:0.220]];
//        fromUserHighlight.userInfo = @{MHCommentUserKey:self.fromUser};
        [mutableAttributedString yy_setTextHighlight:fromUserHighlight range:fromUserRange];
        // 设置昵称颜色
        [mutableAttributedString yy_setColor:color_globalColor range:NSMakeRange(0, self.repliesAgentName.length)];
        
        
        
        NSRange toUserRange = [textString rangeOfString:[NSString stringWithFormat:@"%@:",self.replyAgentName]];
        // 文本高亮模型
        YYTextHighlight *toUserHighlight = [YYTextHighlight highlightWithBackgroundColor:[UIColor colorWithWhite:0.000 alpha:0.220]];
        // 这里痛过属性的userInfo保存User模型，后期通过获取模型然后获取User模型
//        toUserHighlight.userInfo = @{MHCommentUserKey:self.toUser};
        
        [mutableAttributedString yy_setTextHighlight:toUserHighlight range:toUserRange];
        [mutableAttributedString yy_setColor:color_globalColor range:toUserRange];
        
        return mutableAttributedString;
        
    }else{
        
        // 没有回复
        NSString *textString = [NSString stringWithFormat:@"%@: %@", self.repliesAgentName, self.replyAgentContents];
        NSMutableAttributedString *mutableAttributedString = [[NSMutableAttributedString alloc] initWithString:textString];
        mutableAttributedString.yy_font = [UIFont systemFontOfSize:12];
        mutableAttributedString.yy_color = color_textOne;
        mutableAttributedString.yy_lineSpacing = 10.0f;
        
        NSRange fromUserRange = NSMakeRange(0, self.repliesAgentName.length+1);
        // 设置昵称颜色
        [mutableAttributedString yy_setColor:color_globalColor range:fromUserRange];
        
        YYTextHighlight *fromUserHighlight = [YYTextHighlight highlightWithBackgroundColor:[UIColor colorWithWhite:0.000 alpha:0.220]];
//        fromUserHighlight.userInfo = @{MHCommentUserKey:self.fromUser};
        [mutableAttributedString yy_setTextHighlight:fromUserHighlight range:fromUserRange];
        
        return mutableAttributedString;
    }
    
    return nil;
}

- (CGFloat)cellHeight {
    
    // 文本内容
    CGFloat textX = 11.0f;
    CGFloat textY = 7.0f;
    CGSize  textLimitSize = CGSizeMake(kScreenWidth - 2 *textX -20, MAXFLOAT);
    CGFloat textH = [YYTextLayout layoutWithContainerSize:textLimitSize text:self.attributedText].textBoundingSize.height;
    
    self.contentF = (CGRect){{textX , textY} , {textLimitSize.width , textH}};
    
    self.cellHeight = CGRectGetMaxY(self.contentF) + 7.0f;
    
    
    return _cellHeight;
}


@end
